#include "ModuleManagerCLUT.h"

void ModuleManagerCLUT::init(QCVModuleHandlerInterface * parent)
{
	this->parent=parent;
	win=new QWidget();
  layout=new QVBoxLayout(win);
	
	QHBoxLayout * hlayout=new QHBoxLayout();
	QHBoxLayout * hlayout2=new QHBoxLayout();
	QHBoxLayout * hlayout3=new QHBoxLayout();
	le_filename=new QLineEdit();
	pb_open_fuzzy_clut=new QPushButton("Ouvrir Fuzzy CLUT",win);
	pb_save_fuzzy_clut=new QPushButton("Sauver Fuzzy CLUT",win);
	pb_open_clut=new QPushButton("Ouvrir CLUT C11",win);
	pb_save_clut=new QPushButton("Sauver CLUT",win);
	pb_create_distance_clut=new QPushButton(trUtf8("Créer : CLUT distance C11"),win);
	pb_create_entropy_clut_from_fuzzy=new QPushButton(trUtf8("Charger Entropie depuis fuzzy"),win);

	C11_mode=new QRadioButton(tr("mode C11"),win);
	C16_mode=new QRadioButton(tr("mode C16"),win);
	C11_mode->setChecked(true);

	color_mode=new QCheckBox("Mode couleur",win);

	layout->addWidget(le_filename);
	hlayout->addWidget(pb_open_fuzzy_clut);
	hlayout->addWidget(pb_save_fuzzy_clut);
	hlayout2->addWidget(pb_open_clut);
	hlayout2->addWidget(pb_save_clut);
	hlayout3->addWidget(C11_mode);
	hlayout3->addWidget(C16_mode);

	layout->addLayout(hlayout);
	layout->addLayout(hlayout2);
	layout->addLayout(hlayout3);
	layout->addWidget(color_mode);
	layout->addWidget(pb_create_distance_clut);
	layout->addWidget(pb_create_entropy_clut_from_fuzzy);

	connect(pb_open_fuzzy_clut,SIGNAL(clicked()),this,SLOT(s_open_fuzzy_clut()));
	connect(pb_save_fuzzy_clut,SIGNAL(clicked()),this,SLOT(s_save_fuzzy_clut()));
	connect(pb_open_clut,SIGNAL(clicked()),this,SLOT(s_open_clut()));
	connect(pb_save_clut,SIGNAL(clicked()),this,SLOT(s_save_clut()));
	connect(pb_create_distance_clut,SIGNAL(clicked()),this,SLOT(s_create_distance_clut()));
	connect(pb_create_entropy_clut_from_fuzzy,SIGNAL(clicked()),this,SLOT(s_create_entropy_clut_from_fuzzy()));
}
////////////////////////////////////////////////////////////////////////////////
void ModuleManagerCLUT::free()
{
	delete win;
}
////////////////////////////////////////////////////////////////////////////////
cv::Mat & ModuleManagerCLUT::process()
{
		if( !clut11c.empty() || !clut16c.empty() )
		{
			if(color_mode->isChecked())
			{			
				if(C11_mode->isChecked()){RGB_2_RGB11(currentFrame,clut11c,result);}
				if(C16_mode->isChecked()){RGB_2_RGB16(currentFrame,clut16c,result);}
			}
			else
			{
				if(C11_mode->isChecked()){RGB_2_C11(currentFrame,clut11c,result);}
				if(C16_mode->isChecked()){RGB_2_C16(currentFrame,clut16c,result);}
			}
			
			return result;
		}
		else if(!entropyclut.empty())
		{
			RGB_2_GRAY(currentFrame,entropyclut,result);
			return result;
		}	
		else
		{
			return currentFrame;
		}
}
////////////////////////////////////////////////////////////////////////////////
void ModuleManagerCLUT::setInput(int n,const cv::Mat & m)
{
	if(n==0)
	{	
		currentFrame=m.clone();
	}
}
////////////////////////////////////////////////////////////////////////////////
QWidget* ModuleManagerCLUT::widget()
{
	return win;
}
////////////////////////////////////////////////////////////////////////////////
QString ModuleManagerCLUT::getName()
{
	return tr("Manager CLUT");
}
////////////////////////////////////////////////////////////////////////////////
int ModuleManagerCLUT::nbInputs()
{
	return 1;
}
////////////////////////////////////////////////////////////////////////////////
bool ModuleManagerCLUT::setParams(QString params)
{
	QStringList list_params=params.split(";");
		
	if(list_params.size()==1)
	{
		return true;
	}
	else
	{
		return false;
	}	
}
////////////////////////////////////////////////////////////////////////////////
QString ModuleManagerCLUT::getParams()
{
	return "none";
}

QCVModuleInterface * ModuleManagerCLUT::clone()
{
	return new ModuleManagerCLUT;
}
////////////////////////////////////////////////////////////////////////////////
QString ModuleManagerCLUT::help(int id)
{
	if(id==-2)
	{
		return tr("Applique une CLUT Color Look Up Table");
	}
	else if(id==-1)
	{
		return tr("image");
	}
	else if(id==0)
	{
		return tr("image");
	}
	else
	{
		return tr("erreur");
	}
}

void ModuleManagerCLUT::s_direct_open_clut(QString filename)
{
	if(!filename.isEmpty())
	{
		//On ouvre une fuzzyCLUT
		loadFuzzyCLUT(filename,fuzzyclut);

		//On la converti en CLUT simple
		maxFuzzyCLUT_2_CLUT(fuzzyclut,clut11c);
		CLUTC11_2_CLUTC16(clut11c,clut16c);
			
		le_filename->setText(filename);
		C16_mode->setEnabled(true);
		C11_mode->setEnabled(true);

		/*QCVGraph graph;
		getConnexGraph(clut16c,16,10,graph);
		graph.stdPrint();
		for(int i=0;i<16;i++)graph.stdPrintPath(0,i);

		getConnexGraph(clut11c,11,10,graph);
		graph.stdPrint();
		for(int i=0;i<11;i++)graph.stdPrintPath(0,i);*/
	}
}

void ModuleManagerCLUT::s_save_clut()
{
	if( !clut11c.empty() || !clut16c.empty() )
	{
		QString filename=QFileDialog::getSaveFileName(NULL,"Sauver une CLUT",le_filename->text(),"*.txt");
		if(C11_mode->isChecked())saveCLUT(filename,clut11c);
		if(C16_mode->isChecked())saveCLUT(filename,clut16c);
	}
	else
	{
		printf("Erreur : CLUT.data=NULL\n");
	}
}

void ModuleManagerCLUT::s_open_clut()
{
	QString filename=QFileDialog::getOpenFileName(NULL,"Ouvrir une CLUT","data","*.txt");
	
	if(!filename.isEmpty())
	{
		loadCLUT(filename,clut11c);
		CLUTC11_2_CLUTC16(clut11c,clut16c);
		le_filename->setText(filename);
		C16_mode->setEnabled(true);
		C11_mode->setEnabled(true);		
	}
	
	
}

void ModuleManagerCLUT::s_open_fuzzy_clut()
{
	QString filename=QFileDialog::getOpenFileName(NULL,"Ouvrir une Fuzzy CLUT","data","*.txt");
	
	s_direct_open_clut(filename);
}

void ModuleManagerCLUT::s_save_fuzzy_clut()
{
	if(!fuzzyclut.empty())
	{
		QString filename=QFileDialog::getSaveFileName(NULL,"Sauver une CLUT",le_filename->text(),"*.txt");
		saveFuzzyCLUT(filename,fuzzyclut);
	}
	else
	{
		printf("Erreur : fuzzyCLUT.data=NULL\n");
	}
}

void ModuleManagerCLUT::s_create_distance_clut()
{
	int res=0;
	
	bool ok=false;
	res=QInputDialog::getInt ( NULL,trUtf8("Résolution de la CLUT"), trUtf8("Résolution:"), 32,-2147483647, 2147483647, 1, &ok);
	if(ok)
	{
		get_C11_CLUT(clut11c,res);
		CLUTC11_2_CLUTC16(clut11c,clut16c);
		le_filename->setText(tr(""));
	}

	C16_mode->setEnabled(true);
	C11_mode->setEnabled(true);
}

void ModuleManagerCLUT::s_create_entropy_clut_from_fuzzy()
{
	QString filename=QFileDialog::getOpenFileName(NULL,"Ouvrir une CLUT","data","*.txt");
	
	if(!filename.isEmpty())
	{
		//On ouvre une fuzzyCLUT
		loadFuzzyCLUT(filename,fuzzyclut);

		//On la converti en CLUT simple par l'entropie
		entropyFuzzyCLUT_2_CLUT(fuzzyclut,entropyclut);
			
		le_filename->setText(filename);
	}
	clut11c.release();
	clut16c.release();
	C16_mode->setEnabled(false);
	C11_mode->setEnabled(false);
}
////////////////////////////////////////////////////////////////////////////////
Q_EXPORT_PLUGIN2(ModuleManagerCLUT_plugin,ModuleManagerCLUT);  
